/**
 * Copyright (c) 2020-2024, huli.com
 * All rights reserved.
 */
package com.xiaohujr.xuexue.work.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Xue Xue (xuexue1@huli.com)
 * @version 1.0
 * @since 2021/1/14
 */
public class ReadMongoOpLog {

    public static Set<String> getDeleteRecords(String path) throws IOException {
        Set<String> oids = new HashSet<>();
        FileReader reader = new FileReader(path);
        BufferedReader br = new BufferedReader(reader);
        String str = null;
        while ((str = br.readLine()) != null) {
            if (Strings.isNullOrEmpty(str)) {
                continue;
            }
            oids.add(str);
        }
        return oids;
    }

    public static String read(String path, Set<String> oids) throws IOException {
        Set<String> rtnSet = new HashSet<>();
        FileReader reader = new FileReader(path);
        BufferedReader br = new BufferedReader(reader);
        String str = null;
        while ((str = br.readLine()) != null) {
            if (Strings.isNullOrEmpty(str)) {
                continue;
            }
            for (String oid : oids) {
                if (str.contains(oid) && !str.contains("2021-01-13 00:00")) {
                    rtnSet.add(str);
                    System.out.println(str);
                    break;
                }
            }

        }
        br.close();
        reader.close();
        System.out.println("一共有" + rtnSet.size() + "条记录");
        return JSON.toJSONString(rtnSet);
    }


    public static String readandRegx(String path) throws IOException {
        FileReader reader = new FileReader(path);
        BufferedReader br = new BufferedReader(reader);
        String str = null;
        String nameRegex = "(\"loanId\":)(\")(.*?)(\")";
        Pattern namePattern = Pattern.compile(nameRegex, Pattern.CASE_INSENSITIVE);
        int i = 0;
        while ((str = br.readLine()) != null) {
            String loanId = "";
            Matcher m = namePattern.matcher(str);
            while (m.find()) {
                loanId = m.group(3);

                break;
            }
            System.out.println("db.getCollection(\"daiHouPhoneAuditInfo\").update({\"loanId\":\"" + loanId + "\"}," + str);
            i++;
        }
        return String.valueOf(i);
    }

    public static String readandOplog(String path) throws IOException {
        Map<String, Integer> recorda = new HashMap<>();
        Map<String, Integer> recordb = new HashMap<>();

        Map<String,List<String>> insertSQLs = new HashMap<>();
        FileReader reader = new FileReader(path);
        BufferedReader br = new BufferedReader(reader);
        String str = null;
        int i = 0;
        while ((str = br.readLine()) != null) {

            JSONObject jsonObject = JSON.parseObject(str);


            String loanId = jsonObject.getJSONObject("o").getString("loanId");
            String sno = jsonObject.getJSONObject("ts").getJSONObject("$timestamp").getString("t");
            String ori = jsonObject.getJSONObject("ts").getJSONObject("$timestamp").getString("i");
            Integer intSno = Integer.valueOf(sno);
            Integer intOri = Integer.valueOf(ori);

            if (recorda.get(loanId) == null) {
                recorda.put(loanId, intSno);
                recordb.put(loanId, intOri);
            } else {
                if (recorda.get(loanId) > intSno) {
                    System.out.println("t比之前的小哈" + loanId + "----" + recorda.get(loanId)+ "----" + sno);
                    continue;
                } else if (recorda.get(loanId).equals(intSno)) {
                    if (recordb.get(loanId) > intOri) {
                        System.out.println("i比之前的小哈" + loanId+ "----" + recorda.get(loanId) + "----" + ori);
                        continue;
                    }

                }
            }
            recorda.put(loanId, intSno);
            recordb.put(loanId, intOri);

            JSONArray jsonArray = jsonObject.getJSONObject("o").getJSONArray("phoneCallInfos");
            if (jsonArray != null && jsonArray.size() > 0) {
                List<String > list = new ArrayList<>();
                for (int j = 0; j < jsonArray.size(); j++) {

                    JSONObject element = jsonArray.getJSONObject(j);
                    list.add(
                            "\n" +
                                    "db.daiHouPhoneAuditInfo.update({\"loanId\": \"" + loanId + "\"}, " +
                                    "{$addToSet:{\"phoneCallInfos\":" +
                                    element.toJSONString()
                                    + "}},{\"upsert\":true});"
                    );
                    i++;
                }
                insertSQLs.put(loanId,list);
            }

        }

        System.out.println(JSON.toJSONString(recorda));
        System.out.println(JSON.toJSONString(recordb));

        for(List<String> arrays :insertSQLs.values()){
            for(String a:arrays){
                System.out.println(a);
            }
        }

        return String.valueOf(i);
    }

    public static void main(String[] args) throws IOException {
//        Set<String> oids = ReadMongoOpLog.getDeleteRecords("/Users/xuexue/Downloads/deleteRecord.txt");
//
//        ReadMongoOpLog.read("/Users/xuexue/Downloads/risk_manage.daiHouPhoneAuditInfo.update.json", oids);
//
//        ReadMongoOpLog.read("/Users/xuexue/Downloads/risk_manage.daiHouPhoneAuditInfo.insert.json", oids);

//        System.out.println(readandRegx("/Users/xuexue/Downloads/xuexue20210114.json"));
        System.out.println(readandOplog("/Users/xuexue/Downloads/opLogRecover.txt"));


    }
}
